<template>
    <scroll 
      :data='data' 
      class="listview" 
      ref="listview" 

      :listenScroll='listenScroll' 
      @scroll='scroll'
      :probeType='probeType'
    >
      <ul>
        <li class="list-group" v-for="(group,index) of data" :key='index' ref='listGroup'>
          <h2 class="list-group-title">{{group.title}}</h2>
          <ul>
            <li class="list-group-item" 
                v-for="(item,index) of group.items" 
                :key="index"
                @click="toSingerDetail(item)"
            >
              <img v-lazy="item.avatar" class="avatar">
              <span class="name">{{item.name}}</span>
            </li>
          </ul>
        </li>
      </ul>
      <div class="list-shortcut" 
        @touchstart='onShortcutClick'
        @touchmove.stop.prevent='onShortcutOnTouch'
      >
        <ul>
            <li  class='item' v-for="(item,index) of shortcutList" 
              :key='index' 
              :class="{'current':currentNumber==index}"
              :data-index='index'
            >
            {{item}}</li>
        </ul>
      </div>

      <div class="list-fixed" ref="fixed">
        <div class="fixed-title">
            {{fixTitle}}
        </div>
      </div>
    </scroll>
</template>
<script>
import scroll from "base/scroll/scroll";
import {getData} from 'common/js/dom'

const ANCHOR=18

export default {
  created() {
    this.touch={}
    this.listenScroll=true,
    this.listHeight=[],
    this.probeType=3
  },
  data() {
    return {
      scrollY:-1,
      currentNumber:0,
      diff:-1
    }
  },
  props:{
    data:{
      type:Array,
      default:[]
    }
  },
  components:{
    scroll
  },
  computed: {
    shortcutList(){
      //这里会返回 map里的key 的首字母
      return this.data.map((group)=>{
        return group.title.substr(0,1)
      })
    },
    fixTitle(){
      return this.data[this.currentNumber] ? this.data[this.currentNumber].title:''
    } 
  },
  methods: {
    //点击歌手 发送歌手信息
    toSingerDetail(item){
      this.$emit('toSingerDetail', item)

    },
    onShortcutClick(e){

      //接收一个 当前字母的索引
      let currentIndex=getData(e.target,'index')
                // console.log(currentIndex)
      //接收一个 当前触摸位置的 Y坐标值
      this.touch.y1=e.touches[0].pageY

      //保存当前点击字母的索引
      this.touch.currentTouchIndex=currentIndex

      //滚动至目标位置
      this.scrollTo(currentIndex)
    },

    // 右侧 字母栏滑动的事件
    onShortcutOnTouch(e){
      //接收一个 滑动后的坐标值
      this.touch.y2=e.touches[0].pageY

      // 坐标y2-y1 / 字母dom的高度  = 滑动的字母的 个数(索引)
      let distance=Math.floor( ( this.touch.y2 - this.touch.y1 ) / ANCHOR )
      
      //初始坐标索引+滑动后的坐标索引
      let currentIndex =parseInt(this.touch.currentTouchIndex) + distance
      this.scrollTo(currentIndex)
    },
    //better-scroll方法 显示要去的位置
    scrollTo(index){
      //消除 右侧字母栏上下空白处点击效果
      if(index===null){
        return
      }
      if(index<0){
          index=0
      }else if(index>this.listHeight.length-2){
          index=this.listHeight.length-2
      }
      
      this.scrollY=-this.listHeight[index]
      this.$refs.listview.scrollToElement(this.$refs.listGroup[index],0)
    },
    scroll(pos){
      //接收better-scroll 传入的滑动点  坐标
      this.scrollY=pos.y
    },
    //歌手分组 的高度
    computeGroupHeight(){
      //初始化一个保存 歌手分组的 数组
      this.listHeight = []
      
      //得到歌手数组 dom元素
      const array=this.$refs.listGroup
      let height=0
      this.listHeight.push(height)
      //遍历歌手 获取dom元素高度添加至数组
      for (let index = 0; index < array.length; index++) {
        const element = array[index];
        height+=element.clientHeight
        this.listHeight.push(height) 
        
      }
    },

  },
  watch:{
    data(){
      setTimeout(() => {
        this.computeGroupHeight()
      }, 20);
    },

    //时刻监听 this.scrollY值的改变
    scrollY(newY){
      //歌手group 高度
      const list=this.listHeight

      if(newY >0){
        this.currentNumber=0
        return
      }
      //根据scrollY值 取得
      for(let i= 0;i < list.length-1; i++){
        let h1 = list[i]
        let h2 = list[i + 1]
        if ( -newY >= h1  &&  -newY < h2) {
          this.currentNumber = i
          this.diff = h2 + newY
          return
        }
      }
      //左边歌手比字母表少一个
      this.currentNumber=list.length-2
    },
    diff(newVal){
      let fixedTop=(newVal>0 && newVal<30) ? newVal-30 : 0
      console.log(fixedTop)
      if(this.fixedTop===fixedTop){
         return
      }
      this.fixedTop=fixedTop
      this.$refs.fixed.style.transform=`translate3d(0,${fixedTop}px,0)`
    }
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .listview
    position: relative
    width: 100%
    height: 100%
    overflow: hidden
    background: $color-background
    .list-group
      padding-bottom: 30px
      .list-group-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background
      .list-group-item
        display: flex
        align-items: center
        padding: 20px 0 0 30px
        .avatar
          width: 50px
          height: 50px
          border-radius: 50%
        .name
          margin-left: 20px
          color: $color-text-l
          font-size: $font-size-medium
    .list-shortcut
      position: absolute
      z-index: 30
      right: 0
      top: 50%
      transform: translateY(-50%)
      width: 20px
      padding: 20px 0
      border-radius: 10px
      text-align: center
      background: $color-background-d
      font-family: Helvetica
      .item
        padding: 3px
        line-height: 1
        color: $color-text-l
        font-size: $font-size-small
        &.current
          color: $color-theme
    .list-fixed
      position: absolute
      top: -1px
      left: 0
      width: 100%
      .fixed-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background
    .loading-container
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
